//
//  UIView+Extension.m
//  MiaMind
//
//  Created by Vill on 2023/11/23.
//

#import "UIView+Extension.h"
#import <objc/runtime.h>

static char kActionTapBlockKey;
static char kActionTapGestureKey;

@implementation UIView (Extension)

- (void)setX:(CGFloat)x {
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (CGFloat)x {
    return self.frame.origin.x;
}

- (void)setY:(CGFloat)y {
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (CGFloat)y {
    return self.frame.origin.y;
}

- (void)setWidth:(CGFloat)width {
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (CGFloat)width {
    return self.frame.size.width;
}

- (void)setHeight:(CGFloat)height {
    CGRect frame = self.frame;
    frame.size.height= height;
    self.frame = frame;
}

- (CGFloat)height {
    return self.frame.size.height;
}

- (CGFloat)centerX {
    return self.x + self.width/2;
}

- (void)setCenterX:(CGFloat)centerX {
    CGRect frame = self.frame;
    frame.origin.x = centerX - self.width/2;
    self.frame = frame;
}

- (CGFloat)centerY {
    return self.y + self.height/2;
}

- (void)setCenterY:(CGFloat)centerY {
    CGRect frame = self.frame;
    frame.origin.y = centerY - self.height/2;
    self.frame = frame;
}

- (CGFloat)maxX {
    return CGRectGetMaxX(self.frame);
}

- (void)setMaxX:(CGFloat)maxX {
    CGRect frame = self.frame;
    frame.origin.x = maxX - self.width;
    self.frame = frame;
}

- (CGFloat)maxY {
    return CGRectGetMaxY(self.frame);
}

- (void)setMaxY:(CGFloat)maxY {
    CGRect frame = self.frame;
    frame.origin.y = maxY - self.height;
    self.frame = frame;
}

- (void)addTapGestureWithBlock:(nonnull GestureActionBlock)block {
    self.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = objc_getAssociatedObject(self, &kActionTapGestureKey);
    if (!tap) {
        tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTapAction:)];
        [self addGestureRecognizer:tap];
        objc_setAssociatedObject(self, &kActionTapGestureKey, tap, OBJC_ASSOCIATION_RETAIN);
    }
    objc_setAssociatedObject(self, &kActionTapBlockKey, block, OBJC_ASSOCIATION_COPY);
}

- (void)handleTapAction:(UITapGestureRecognizer *)tap {
    if (tap.state == UIGestureRecognizerStateRecognized) {
        GestureActionBlock block = objc_getAssociatedObject(self, &kActionTapBlockKey);
        if (block) {
            block(tap);
        }
    }
}

- (void)clipsCorners:(UIRectCorner)corners radius:(CGFloat)radius {
    UIBezierPath *maskPath;
    maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds
                                     byRoundingCorners:corners
                                           cornerRadii:CGSizeMake(radius, radius)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.bounds;
    maskLayer.path = maskPath.CGPath;
    self.layer.mask = maskLayer;
}

-(void)setupRadiu:(CGFloat)radiu corner:(UIRectCorner)corner{
    //圆角处理
    if (@available(iOS 11.0, *)) {
        self.layer.cornerRadius = radiu;
        self.layer.maskedCorners = (CACornerMask)corner;
        self.layer.masksToBounds = YES;
        
    } else {
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:corner cornerRadii:CGSizeMake(radiu, radiu)];
        CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
        maskLayer.frame = self.bounds;
        maskLayer.path = maskPath.CGPath;
        self.layer.mask = maskLayer;
    }
}

-(void)setupRadiu:(CGFloat)radiu{
    [self setupRadiu:radiu corner:UIRectCornerAllCorners];
}

@end
